﻿using System.IO;
using System.Linq;
using HK.Core.Utils;
using HK.Editor.BuildSystem;
using HK.Editor.Common;
using HK.Editor.Defines.Data;
using HK.Editor.Defines.Window;
using UnityEditor;
using HLogger = HK.Core.Logs.Logger;

namespace HK.Editor.Defines {
	
	/// <summary>
	/// 宏设定.
	/// </summary>
	public static class DefinesSetting 
	{

		/// <summary>
		/// 追加宏 - iOS.
		/// </summary>
		public static void AddIOSDefines() {

			var defines = BuildParameters.Defines;
			SetDefines (defines, BuildTargetGroup.iOS);
		}

		/// <summary>
		/// 追加宏 - Android.
		/// </summary>
		public static void AddAndroidDefines() {

			var defines = BuildParameters.Defines;
			SetDefines (defines, BuildTargetGroup.Android);
		}
		
		/// <summary>
		/// 追加宏 - Pc.
		/// </summary>
		public static void AddPcDefines() {

			var defines = BuildParameters.Defines;
			SetDefines (defines, BuildTargetGroup.Standalone);
		}
		
		/// <summary>
		/// 追加宏 - Ps4.
		/// </summary>
		public static void AddPs4Defines() {

			var defines = BuildParameters.Defines;
			SetDefines (defines, BuildTargetGroup.PS4);
		}
		
		/// <summary>
		/// 追加宏 - Ps5.
		/// </summary>
		public static void AddPs5Defines() {

			var defines = BuildParameters.Defines;
			SetDefines (defines, BuildTargetGroup.PS5);
		}
		
		/// <summary>
		/// 追加宏 - XBoxOne.
		/// </summary>
		public static void AddXboxOneDefines() {

			var defines = BuildParameters.Defines;
			SetDefines (defines, BuildTargetGroup.XboxOne);
		}

		/// <summary>
		/// 取得宏数据路径
		/// </summary>
		/// <returns>宏数据路径</returns>
		private static string GetDefinesDataPath()
		{
			const string dataDir = EditorConst.WIN_FILE_DATA_DIR;
			var dataPath = $"{dataDir}/{nameof(DefinesWindow)}.json";
			return dataPath;
		}

		/// <summary>
		/// 取得已有宏数据
		/// </summary>
		/// <returns>已有宏数据</returns>
		private static DefinesData GetDefinesData(string iPath)
		{
			if (string.IsNullOrEmpty(iPath) || File.Exists(iPath)) return null;
			
			return UtilsSAssets.ImportFromJsonByPathSync<DefinesData>(iPath);
		}
			
		/// <summary>
		/// 设定宏.
		/// </summary>
		/// <param name="iDefines">宏.</param>
		/// <param name="iTargetGroup">目标组.</param>
		private static void SetDefines(string[] iDefines, BuildTargetGroup iTargetGroup) 
		{

			if (null == iDefines || 0 >= iDefines.Length) {
				return;
			}

			var dataPath = GetDefinesDataPath();
			// 追加
			var definesData = GetDefinesData(dataPath) ?? new DefinesData();
			definesData.AddDefines (iDefines, iTargetGroup, true);

			// 重新导出
			UtilsSAssets.ExportToJsonSync(definesData, dataPath);

			// 应用设定信息
			definesData.Apply();

		}
			
		/// <summary>
		/// 设定宏.
		/// </summary>
		/// <param name="iDefines">宏.</param>
		/// <param name="iTargetGroup">目标平台类型.</param>
		public static void AppDefinesToPlatform(DefineInfo[] iDefines, BuildTargetGroup iTargetGroup)
		{

			var defines = "";
			if (null != iDefines && 0 < iDefines.Length) {
				defines = iDefines
					.Where(iDefine => !string.IsNullOrEmpty(iDefine.Define))
					.Aggregate<DefineInfo, string>(null, 
						(iCurrent, iDefine) => string.IsNullOrEmpty(iCurrent) ? 
							iDefine.Define : $"{iCurrent};{iDefine.Define}");
			}

			// 设定
			PlayerSettings.SetScriptingDefineSymbolsForGroup (iTargetGroup, defines); 
			HLogger.BuildLog($"DefinesSetting()::Defines({iTargetGroup}):{defines}");
		}

		/// <summary>
		/// 取得指定平台的宏设定
		/// </summary>
		/// <param name="iTargetGroup">目标平台类型.</param>
		/// <returns>宏设定</returns>
		public static string GetDefinesByBuildTargetGroup(BuildTargetGroup iTargetGroup)
		{
			return PlayerSettings.GetScriptingDefineSymbolsForGroup(iTargetGroup);
		}
	}
}
